import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;

public class Log extends JPanel {
	private JTextArea chat, log, chatlog;
	private LogListener logListener;
	final JTextField chatField;
	
	public Log() {
		setLayout(new BorderLayout());
		JTabbedPane logPane = new JTabbedPane();
		chat = new JTextArea();
		chat.setEditable(false);
		log = new JTextArea();
		log.setEditable(false);
		chatlog = new JTextArea();
		chatlog.setEditable(false);
		logPane.add("Chat", new JScrollPane(chat));
		logPane.add("Log", new JScrollPane(log));
		logPane.add("Chat & Log", new JScrollPane(chatlog));
		logPane.setPreferredSize(new Dimension(215,200));
		//logPane.setMinimumSize(new Dimension(215,100));
		add(logPane, BorderLayout.CENTER);
		JPanel chatPanel = new JPanel();
		chatPanel.setLayout(new BorderLayout());
		//SubmitButton and ActionListener
		chatField = new JTextField();
		chatField.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent arg0) {
				if(arg0.getKeyCode() == KeyEvent.VK_ENTER){
					handleSubmit();
				}
				
			}

			@Override
			public void keyReleased(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		JButton submitChat = new JButton("Submit");
		submitChat.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				handleSubmit();
			}
			
		});
		chatPanel.add(chatField, BorderLayout.CENTER);
		chatPanel.add(submitChat, BorderLayout.LINE_END);
		add(chatPanel, BorderLayout.PAGE_END);
	}
	
	private void handleSubmit(){
		Game.getGame().client.sendChat(chatField.getText());
		chatField.setText("");
	}
	
	public void addChat(String entry) {
		chat.append(entry+"\n");
		chatlog.append(entry+"\n");
	}
	public void addLog(String entry) {
		log.append(entry+"\n");
		chatlog.append(entry+"\n");
	}
	
	public LogListener LogListener(){
		return logListener;
	}
	
}
